package ants.p2p.utils.net;

import ants.p2p.filesharing.WarriorAnt;
import java.io.*;

import org.apache.log4j.*;
import kerjodando.vincent.Common;
import java.util.Random;

public class UPnPDescriptor {

	static Logger _logger = Logger.getLogger(WarriorAnt.class.getName());

	public static String getUPnPDeviceDescriptor() {
		String Devname = "kerjo" + Common.identifier_id
				+ (new Random(System.currentTimeMillis())).nextInt();
		if (Devname.length() > 64)
			Devname = Devname.substring(0, 64);
		String decriptor = "<?xml version=\"1.0\" ?>\n"
				+ "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n"
				+ "<specVersion>\n"
				+ "<major>1</major>\n"
				+ "<minor>0</minor>\n"
				+ "</specVersion>\n"
				+ "<device>\n"
				+ "<deviceType>urn:schemas-upnp-org:device:kerjodando:1</deviceType>\n"
				+
				// "<friendlyName>Kerjodando Device</friendlyName>\n"+
				"<friendlyName>"
				+ Devname
				+ "</friendlyName>\n"
				+ "<manufacturer>Kerjodando development team</manufacturer>\n"
				+ "<manufacturerURL>Kerjodando</manufacturerURL>\n"
				+ "<modelDescription>Kerjodando Device</modelDescription>\n"
				+ "<modelName>Kerjodando "
				+ WarriorAnt.getVersion()
				+ "["
				+ WarriorAnt.getProtocolVersion()
				+ "]</modelName>\n"
				+ "<modelNumber>"
				+ WarriorAnt.getVersion()
				+ "["
				+ WarriorAnt.getProtocolVersion()
				+ "]</modelNumber>\n"
				+ "<modelURL>http://www.kerjodando.com/</modelURL>\n"
				+ "<serialNumber>"
				+ System.currentTimeMillis()
				+ "</serialNumber>\n"
				+
				// "<UDN>uuid:Kerjodando"+System.currentTimeMillis()+"</UDN>\n"+
				"<UDN>uuid:Kerjodando"
				+ Common.identifier_id
				+ "</UDN>\n"
				+ "<UPC>"
				+ System.currentTimeMillis()
				+ "</UPC>\n"
				+ "<iconList>\n"
				+ "<icon>\n"
				+ "<mimetype>image/gif</mimetype>\n"
				+ "<width>16</width>\n"
				+ "<height>16</height>\n"
				+ "<depth>8</depth>\n"
				+ "<url>"
				+ _logger.getClass().getClassLoader().getResource(
						"anims/ant_ico0.gif")
				+ "</url>\n"
				+ "</icon>\n"
				+ "</iconList>\n"
				+ "<serviceList>\n"
				+ "<service>\n"
				+ "<serviceType>urn:schemas-upnp-org:service:address:1</serviceType>\n"
				+ "<serviceId>urn:schemas-upnp-org:serviceId:address:1</serviceId>\n"
				+ "<SCPDURL>/service/address/description.xml</SCPDURL>\n"
				+ "<controlURL>/service/address/control</controlURL>\n"
				+ "<eventSubURL>/service/address/eventSub</eventSubURL>\n"
				+ "</service>\n"
				+ "</serviceList>\n"
				+ "<presentationURL>http://www.kerjodando.com/</presentationURL>\n"
				+ "</device>\n" + "</root>\n";
		return decriptor;
	}

	public static String getUPnPServiceDescriptor() {
		String decriptor = "<?xml version=\"1.0\"?>\n"
				+ "<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">\n"
				+ "<specVersion>\n" + "<major>1</major>\n"
				+ "<minor>0</minor>\n" + "</specVersion>\n" + "<actionList>\n"
				+ "<action>\n" + "<name>GetLANAddress</name>\n"
				+ "<argumentList>\n" + "<argument>\n"
				+ "<name>LANAddress</name>\n"
				+ "<relatedStateVariable>LANAddress</relatedStateVariable>\n"
				+ "<direction>out</direction>\n" + "</argument>\n"
				+ "</argumentList>\n" + "</action>\n" + "</actionList>\n"
				+ "<serviceStateTable>\n"
				+ "<stateVariable sendEvents=\"yes\">\n"
				+ "<name>LANAddress</name>\n" + "<dataType>string</dataType>\n"
				+ "</stateVariable>\n" + "<stateVariable sendEvents=\"no\">\n"
				+ "<name>Result</name>\n" + "<dataType>string</dataType>\n"
				+ "</stateVariable>\n" + "</serviceStateTable>\n" + "</scpd>";
		return decriptor;
	}

	public static void createDirectoryStructure() {
		File upnpDescriptorDir = new File(WarriorAnt.workingPath
				+ "upnpdescriptor/");
		if (!upnpDescriptorDir.exists()) {
			upnpDescriptorDir.mkdir();
		}

		File upnpDescriptor = new File(WarriorAnt.workingPath
				+ "upnpdescriptor/description.xml");
		try {
			FileOutputStream fos = new FileOutputStream(upnpDescriptor);
			PrintWriter pw = new PrintWriter(fos);
			pw.print(getUPnPDeviceDescriptor());
			pw.close();
		} catch (IOException e) {
			_logger.error("Cannot create UPnP Device descriptor", e);
		}
		File upnpServiceDescriptorDir = new File(WarriorAnt.workingPath
				+ "upnpdescriptor/service/");
		if (!upnpServiceDescriptorDir.exists()) {
			upnpServiceDescriptorDir.mkdir();
		}
		File upnpLANAddressDescriptorDir = new File(WarriorAnt.workingPath
				+ "upnpdescriptor/service/address");
		if (!upnpLANAddressDescriptorDir.exists()) {
			upnpLANAddressDescriptorDir.mkdir();
		}
		File upnpServiceDescriptor = new File(WarriorAnt.workingPath
				+ "upnpdescriptor/service/address/description.xml");
		try {
			FileOutputStream fos = new FileOutputStream(upnpServiceDescriptor);
			PrintWriter pw = new PrintWriter(fos);
			pw.print(getUPnPServiceDescriptor());
			pw.close();
		} catch (IOException e) {
			_logger.error("Cannot create UPnP Service descriptor", e);
		}
	}
}
